/*
  Name: DeadProxyClientVip.cpp
  Copyright:
  Author: Carl Chatfield
  Date: 17/02/08 13:32
  Description: Stores vip list
*/

#include "DeadProxyClientVIP.h"
#include "DeadProxyApp.h"



bool VIPList::AlphaGreaterTest(char * s1, char * s2)     //If fist is greater than second return true
{
    int i = 0;
    int val1;
    int val2;
    while(true)
    {
        if(s1[i] >= 97 && s1[i] <= 122)                     //If it is a lower case character
        {
            val1 = s1[i] - 32;                              //Convert it to upper case
        } else {
            val1 = s1[i];
        }
        if(s2[i] >= 97 && s2[i] <= 122)                     //If it is a lower case character
        {
            val2 = s2[i] - 32;                              //Convert it to upper case
        } else {
            val2 = s2[i];
        }
        if(val1 == 0)
        {
            return false;
        }
        if(val2 == 0)
        {
            return true;
        }
        if(val1 < val2)
        {
            return false;
        }
        if(val1 > val2)
        {
            return true;
        }
        i++;
    }
}
            

bool VIPList::InsertVIP(unsigned int TibiaId, char * Name, bool Online)
{
    if(List.Count == 0)
    {
        List.list[0].TibiaId = TibiaId;
        strcpy(List.list[0].Name, Name);
        List.list[0].Online = Online;
        List.Count++;
        return true;
    }
    if(List.Count == 200)
    {
        return false;
    }
    int InsPos = 0;
    while(AlphaGreaterTest(Name, List.list[InsPos].Name) && InsPos < List.Count)
    {
        InsPos++;
    }
    memcpy(&List.list[InsPos + 1], &List.list[InsPos], sizeof(VIPInfo) * (List.Count - InsPos));
    List.list[InsPos].TibiaId = TibiaId;
    strcpy(List.list[InsPos].Name, Name);
    List.list[InsPos].Online = Online;
    List.Count++;
    return true;
}

bool VIPList::DeleteVIP(unsigned int TibiaId)
{
    int DelPos = 0;
    while(DelPos < List.Count)
    {
        if(List.list[DelPos].TibiaId == TibiaId)
        {

            memcpy(&List.list[DelPos], &List.list[DelPos + 1], sizeof(VIPInfo) * (List.Count - DelPos));
            DelPos++;
            List.Count--;
            return true;
        }
        DelPos++;
    }
    return false;
}
    
void VIPList::AddVIP(unsigned int TibiaId, char * Name, bool Online)
{
    InsertVIP(TibiaId, Name, Online);
}

void VIPList::RemVIP(unsigned int TibiaId)
{
    DeleteVIP(TibiaId);
}

void VIPList::VIPLogin(unsigned int TibiaId)
{
    int pos = 0;
    while(pos < List.Count)
    {
        if(List.list[pos].TibiaId == TibiaId)
        {
            List.list[pos].Online = true;
        }
    }
}

void VIPList::VIPLogout(unsigned int TibiaId)
{
    int pos = 0;
    while(pos < List.Count)
    {
        if(List.list[pos].TibiaId == TibiaId)
        {
            List.list[pos].Online = false;
        }
    }
}
    
VIPList::VIPList()
{
    List.Count = 0;
//    AddVIP(1, "Anna", false);
//    AddVIP(2, "Annabell", false);
//    AddVIP(3, "Beowulf on libera", false);
//    AddVIP(4, "Beowulf on venore", false);
//    AddVIP(5, "Charlie", false);
//    AddVIP(6, "Charlie Brown", false);
//    AddVIP(7, "Br", false);
//    AddVIP(8, "Pl", false);
//
////    RemVIP(1);
////    RemVIP(8);
////    RemVIP(3);
//    wxString * output = new wxString;
//    output->Printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", List.list[0].Name, List.list[1].Name, List.list[2].Name, List.list[3].Name, List.list[4].Name, List.list[5].Name, List.list[6].Name, List.list[7].Name);
//    app->LogMessage(output);

}
